<template>
  <div class="cartDetail">
    <div class="content">
      <van-checkbox-group v-model="checked" @change="groupChange">
        <div v-for="(item, index) in carStore.cartList" :key="index">
          <ListItem
            :item="item"
            :handelChange="handelChange"
            :showCheckBox="true"
          >
          </ListItem>
        </div>
      </van-checkbox-group>
    </div>
    <van-submit-bar
      :price="AllPrice"
      button-text="结算"
      @submit="onSubmit"
      button-color="#ffc400"
      v-if="carStore.isDelete"
    >
      <van-checkbox
        v-model="submitChecked"
        checked-color="#ffc400"
        @click="choseAll"
        >全选</van-checkbox
      >
    </van-submit-bar>
    <div class="buy" v-else>
      <div class="left">
        <van-checkbox
          v-model="submitChecked"
          checked-color="#ffc400"
          @click="choseAll"
          >全选</van-checkbox
        >
      </div>
      <div class="delete" @click="handleDelete">删除</div>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted, ref} from "vue";
import { useCartStore } from "../../../store/CartSore";
import ListItem from "../../../components/ListItem.vue";
import { useRouter } from "vue-router";
import { List } from "vant";
const props=defineProps({
  changeShow:Function
})
const router=useRouter()
const checked = ref([]);
const carStore = useCartStore();
const submitChecked = ref(true);
const AllPrice = computed(() => {
  let countList = updateData();
  let sum = 0;
  countList.forEach((item) => (sum += item.num * item.price));
  return sum * 100;
});
const onSubmit = () => {
  if(checked.value.length){
    carStore.pay(updateData())
    router.push({
      path:"/createorder",
      query:{
        cartList:checked.value
      }
    })
  }
  else{
    showFailToast("请选择商品");
  }
};
const choseAll = () => {
  if (checked.value.length !== carStore.cartList.length) {
    init();
  } else {
    checked.value = [];
  }
};
const groupChange = (result) => {
  if (result.length === carStore.cartList.length) {
    submitChecked.value = true;
  } else {
    submitChecked.value = false;
  }
  checked.value = result;
};
const handelChange = (value, detail) => {
  carStore.cartList.map((item) => {
    if (item.id === detail.name) {
      item.num = value;
    }
  });
};
const updateData = (type) => {
  return carStore.cartList.filter((item) => {
    return type === "delete"
      ? !checked.value.includes(item.id)
      : checked.value.includes(item.id);
  });
};
const handleDelete = () => {
  if (checked.value.length) {
    carStore.deleteCart(updateData("delete"));
    checked.value = [];
    if(!carStore.cartList.length){
      carStore.changeEdit("delete")
      props.changeShow()
    }
  } else {
    showFailToast("请选择要删除的商品");
  }
};
const init = () => {
  checked.value = carStore.cartList.map((item) => item.id);
};
onMounted(() => {
  init();
});
</script>

<style scoped lang="less">
.cartDetail {
  font-size: 14px;
  flex: 1;
  position: relative;
  overflow-y: auto;
  padding: 20px 20px 55px;
  .content {
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;
    .submit-all {
      position: fixed;
      bottom: 46px;
    }
  }
  .buy {
    position: fixed;
    bottom: 90px;
    right: 0;
    display: flex;
    justify-content: space-between;
    width: 100%;
    background-color: #fff;
    border-radius: 10px;
    height: 80px;
    align-items: center;
    padding: 0 16px;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
    }
    .delete {
      color: #fff;
      background-color: #ffc400;
      border-radius: 20px;
      font-size: 14px;
      font-weight: 600;
      width: 110px;
      height: 40px;
      text-align: center;
      line-height: 40px;
    }
  }
}
</style>
